home *** CD-ROM | disk | FTP | other *** search
/ Mac Power 1997 December / MACPOWER-1997-12.ISO.7z / MACPOWER-1997-12.ISO / AMUG / MAC OS 8 / cmm-framework-10.sit / Trygve's CMM Framework 1.0 / PluginTemplate / Plugin.cp < prev    next >
Text File  |  1997-08-28  |  9KB  |  224 lines

  1. //
  2. // Copyright ゥ1997 by Trygve Isaacson. All Rights Reserved.
  3. //
  4. // Additional Restrictions:
  5. //
  6. // Permission granted to:
  7. // - Compile this source code.
  8. // - Modify this source code, but not to remove this copyright information.
  9. // - Freely distribute the compiled object form of this source code, and any
  10. //   modifications you make, in your programs.
  11. //
  12. // Permission NOT granted to:
  13. // - Redistribute modified versions of this source code.
  14. //
  15. // You grant Trygve Isaacson one single-user license to your plugins based
  16. // on this source code, and notify him at:
  17. // trygve@bombaydigital.com
  18. //
  19.  
  20. #include "Plugin.h"
  21.  
  22. #undef Inherited
  23. #define Inherited PluginBase
  24.  
  25. #define    kBeepCommand    0
  26.  
  27. Plugin::Plugin()
  28.     {
  29.     }
  30.  
  31. Plugin::~Plugin()
  32.     {
  33.     }
  34.  
  35. UInt32 Plugin::GetPluginDataTypes(OSType** pluginTypesArrayPtr, Boolean* lookInLists)
  36.     {
  37. //    You only have to override this if you process a data type other than typeFSS. The
  38. //    base class fills in a single array element for typeFSS.
  39.  
  40. //    EXAMPLE OVERRIDE:
  41. //    const UInt32 kMyNumTypes = 2;    // I have two data types I process.
  42. //    *pluginTypesArrayPtr = (OSType*) ::NewPtr(kMyNumTypes * sizeof(OSType));
  43. //    *pluginTypesArrayPtr[0] = typeFSS;    // I can process files.
  44. //    *pluginTypesArrayPtr[1] = typeChar;    // I can process raw text.
  45. //    *lookInLists = true;
  46. //    return kMyNumTypes;
  47.  
  48.     const UInt32 kMyNumTypes = 1;
  49.     *pluginTypesArrayPtr = (OSType*) ::NewPtr(kMyNumTypes * sizeof(OSType));
  50.     *pluginTypesArrayPtr[0] = typeFSS;    // I can process file specs.
  51.     *lookInLists = true;
  52.     return kMyNumTypes;
  53.     }
  54.     
  55. Boolean Plugin::AddMultiDataTypeCommand(AEDescList* commandList, UInt32 /*numDataTypes*/, OSType* /*dataTypesArray*/, UInt32 numDataTypesFound)
  56.     {
  57. //    You will normally use this override, assuming you use a given command for several
  58. //    data types, or your one-and-only data type.
  59.  
  60. //    EXAMPLE OVERRIDE:
  61. //    Suppose you have 1 command that you use if ANY desired types was found:
  62. //    if (numDataTypesFound > 0)
  63. //        this->AddCMMCommand(commandList, "¥pDo My Stuff", kDoMyStuffCommand);
  64. //    return true;    // true means do NOT call AddDataTypeCommand for individual data types
  65.  
  66. //    If you don't override this message, or if you return false from it, you must
  67. //    override AddDataTypeCommand, which is the next function below.
  68.  
  69.     if (numDataTypesFound > 0)
  70.         this->AddCommand(commandList, "¥pBeep", kBeepCommand);
  71.  
  72.     return true;    // true means do NOT call AddDataTypeCommand for individual data types
  73.     }
  74.  
  75. void Plugin::AddDataTypeCommand(AEDescList* /*commandList*/, OSType /*dataType*/)
  76.     {
  77. //    You will need to use this override if you operate on multiple data types, and you
  78. //    don't use the same command for all types. You will use this override to add
  79. //    the command(s) that apply to the supplied data type, or to remember (through a
  80. //    Plugin class instance variable) that the supplied type was found, so that you
  81. //    can add the commands at the end of the Examine phase.
  82.  
  83. //    EXAMPLE OVERRIDE:
  84. //    Suppose you use different commands depending on what's selected, for example,
  85. //    you have one command for operating on FSSpec's, and a different command for
  86. //    TEXT data in the AEDesc.
  87. //    if (dataType == typeFSS)
  88. //        this->AddCMMCommand(commandList, "¥pBash File", kBashCommand);
  89. //    else if (dataType == typeChar)
  90. //        this->AddCMMCommand(commandList, "¥pMunge Text", kMungeTextCommand);
  91.  
  92. //    If you don't override this message, you must override AddMultiDataTypeCommand,
  93. //    which is the previous function above.
  94.     }
  95.  
  96. OSType Plugin::GetCoercionTypeForCommand(SInt32 /*commandID*/)
  97.     {
  98. //    You only have to override this if you process a data type other than typeFSS. The
  99. //    base class returns typeFSS.
  100.  
  101. //    EXAMPLE OVERRIDE:
  102. //    You have to override this to tell the base class how to coerce that AEDesc
  103. //    data. If you only process a single type, return it. Otherwise, check the
  104. //    commandID to see what command you are dealing with. Suppose you have a couple
  105. //    of data types as shown in the examples above:
  106. //    if (commandID == kBashCommand)
  107. //        return typeFSS;
  108. //    else //if (commandID == kMungeTextCommand)
  109. //        return typeChar;
  110.  
  111.     return typeFSS;
  112.     }
  113.  
  114. OSStatus Plugin::ProcessSelectionItem(AEDesc* /*coercedDescriptor*/, SInt32 /*commandID*/)
  115.     {
  116. //    If you process non-typeFSS data, this is where you get to do your work. If you
  117. //    process typeFSS data, you can use the "AlterXXXXX" methods instead, which get
  118. //    called by the base class after some file management has been done for you.
  119.  
  120. //    EXAMPLE OVERRIDE:
  121. //    If you're doing FSSpec's and not other AEDesc data, you don't have to override
  122. //    this. But if you do non-FSSpec data, this is where you get each item and
  123. //    process it.
  124.  
  125.     ::SysBeep(5);
  126.  
  127.     return noErr;
  128.     }
  129.  
  130. Boolean Plugin::IsDesiredFSSpec(const FSSpec* aFileSpec, SInt32 /*commandID*/, Boolean /*isExaminePhase*/)
  131.     {
  132. //    If you are processing typeFSS data, but only want certain files, you need to
  133. //    override this method. If you depend only the file type/creator being right,
  134. //    you can call PluginBase::IsSpecificCreatorType to find out. If you depend on
  135. //    other file attributes, you'll have to do something like GetFInfo on the file.
  136. //    The isExaminePhase parameter tells you whether the file is being checked during
  137. //    the menu building (examination) phase or the command processing phase; you might
  138. //    use it in special cases.
  139. //    Don't forget, folders are also FSSpec's. There's a method PluginBase::IsFolder
  140. //    that can be used to check for folders.
  141.  
  142. //    EXAMPLE OVERRIDE:
  143. //    Suppose I process FSSpecs, but only text files and applications, and also
  144. //    suppose that I don't want to spend time checking this while examining files,
  145. //    only when actually processing files. In other words, during the examine phase,
  146. //    I'll accept any FSSpec in the selection as reason to add my command to the menu,
  147. //    but during the execution phase, I'll skip any inappropriate files.
  148. //    return ((! isExaminePhase) &&
  149. //            (this->IsSpecificCreatorType(aFileSpec, '****', 'TEXT') ||
  150. //            this->IsSpecificCreatorType(aFileSpec, '****', 'APPL'));
  151.  
  152. //    In this example, we skip folders and just do files.
  153. /*
  154.     Str255    bug = "¥pCreator[xxxx], Type[xxxx]";
  155.     FInfo    info;
  156.     (void) ::FSpGetFInfo(aFileSpec, &info);
  157.     ::BlockMove(&info.fdCreator, &bug[9], 4);
  158.     ::BlockMove(&info.fdType, &bug[21], 4);
  159.     ::DebugStr(bug);
  160. */
  161.     return ! this->IsSpecificCreatorType(aFileSpec, '****', 'TEXT');
  162.     }
  163.  
  164. Boolean Plugin::ModifiesFileInfo(FSSpec* /*aFile*/, SInt32 /*commandID*/)
  165.     {
  166. //    You only have to override this if you return true. This indicates that
  167. //    you will modify the FInfo of the file, so the FInfo is gotten and set,
  168. //    and you just have to tweak the contents of the FInfo in your override
  169. //    of AlterFileInfo.
  170.     return true;
  171.     }
  172.  
  173. Boolean Plugin::ModifiesDataFork(FSSpec* /*aFile*/, SInt32 /*commandID*/)
  174.     {
  175. //    You only have to override this if you return true. This indicates that
  176. //    you will modify the data fork of the file, so the temp file is created
  177. //    and opened for you, etc. In this example we are just modifying the FInfo.
  178.     return false;
  179.     }
  180.  
  181. Boolean Plugin::ModifiesResourceFork(FSSpec* /*aFile*/, SInt32 /*commandID*/)
  182.     {
  183. //    You only have to override this if you return true. This indicates that
  184. //    you will modify the resource fork of the file, so the temp file is created
  185. //    and opened for you, etc. In this example we are just modifying the FInfo.
  186.     return false;
  187.     }
  188.  
  189. Boolean Plugin::AlterFileInfo(FInfo* /*fileInfo*/, FSSpec* /*aFile*/, SInt32 /*commandID*/)
  190.     {
  191. //    You will only override this if you return true for ModifiesFileInfo. You are
  192. //    given the existing FInfo of the file. You modify it and return true. If for
  193. //    some reason you don't actually modify it, return false, so that it is not
  194. //    written back to disk.
  195.     return false;
  196.     }
  197.  
  198. OSStatus Plugin::AlterDataFork(SInt16 /*origFileRefnum*/, SInt16 /*tempFileRefnum*/, SInt32 /*commandID*/)
  199.     {
  200. //    Since our overrides above indicate that we don't touch the data fork, this
  201. //    override won't actually be called, and isn't needed. But if you indicate that
  202. //    you modify the data fork, this is called for you to FSRead data from the
  203. //    origFileRefnum (the original file), process it, and FSWrite it to the
  204. //    tempFileRefnum (the temp file). If anything goes wrong, return an error result
  205. //    and everything will be cleaned up so the original file is left untouched. If
  206. //    everything succeeds, the temp file and the original are exchanged, then the
  207. //    original is deleted.
  208.     return noErr;
  209.     }
  210.  
  211. OSStatus Plugin::AlterResourceFork(SInt16 /*origFileRefnum*/, SInt16 /*tempFileRefnum*/, SInt32 /*commandID*/)
  212.     {
  213. //    Since our overrides above indicate that we don't touch the resource fork, this
  214. //    override won't actually be called, and isn't needed. But if you indicate that
  215. //    you modify the resource fork, this is called for you to get resources from the
  216. //    origFileRefnum (the original file), do your stuff, and write resources to the
  217. //    tempFileRefnum (the temp file). Make sure you call UseResFile as appropriate.
  218. //    If anything goes wrong, return an error result and everything will be cleaned
  219. //    up so the original file is left untouched. If everything succeeds, the temp file
  220. //    and the original are exchanged, then the original is deleted.
  221.     return noErr;
  222.     }
  223.  
  224.